package com.breeze.components.oss.service;

import static com.breeze.components.oss.mybaits.entity.table.FilePartDetailTable.FILE_PART_DETAIL;

import com.breeze.components.oss.mybaits.entity.FilePartDetailDo;
import com.breeze.components.oss.mybaits.mapper.FilePartDetailMapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import java.util.Objects;
import org.dromara.x.file.storage.core.upload.FilePartInfo;
import org.springframework.stereotype.Service;

/**
 * 文件分片信息表，仅在手动分片上传时使用 服务层实现。
 *
 * @author breeze
 * @since 2024-05-13 10:08:23
 */
@Service
public class FilePartDetailService extends ServiceImpl<FilePartDetailMapper, FilePartDetailDo> {

  /**
   * 保存文件分片信息
   *
   * @param info 文件分片信息
   */
  public void saveFilePart(FilePartInfo info) {
    var detail = FilePartDetailDo.toFilePartDetail(info);
    if (save(detail) && Objects.nonNull(detail)) {
      info.setId(String.valueOf(detail.getId()));
    }
  }

  /** 删除文件分片信息 */
  public void deleteFilePartByUploadId(String uploadId) {
    remove(query().where(FILE_PART_DETAIL.UPLOAD_ID.eq(uploadId)));
  }
}
